// 单独的创建一个中间件，然后在app.js中注册使用
const jwt = require('jsonwebtoken')
async function check(ctx, next) {
    let url = ctx.url.split('?')[0]
    console.log(url)
    // 如果是登陆页面和注册页面就不需要验证token了 
    if (

        
        url === '/api/file/file'||
        url === '/api/eu/login'||
        url === '/api/eu/activation'||
        url === '/api/eu/register'||
        url === '/api/wx'||
        url === '/api/wx/wxLogin'||
        url === '/api/nav/list'||
        url === '/api/article/list'||
        url === '/api/article/tlist' ||
        url === '/api/article/hot' ||
        url === '/api/article/detail' ||
        url === '/api/user/detail' ||
        url === '/api/ctype/list' ||
        url === '/api/link/list' ||
        url === '/api/account/register' ||
        url === '/api/account/login' ||
        url === '/api/line/list' ||
        url === '/api/record/add' ||
        url === '/api/record/list' ||
        url === '/api/record/del' ||
        url === '/api/commont/list'||
        url === '/api/commont/news'||
        url === '/api/account/login'||
        url === '/api/commont/add'||
        url === '/api/account/login'||
        url === '/api/tool/tel'||
        url === '/api/tool/zm'||
        url === '/api/tool/strsLs'||
        url === '/api/share/list'
        // url === '/api/record/recordAll' || 
        // url === '/article/list' || 
        // url === '/typeinfo/typeAll' || 
        // url === '/record/recordEditById' ||
        // url === '/record/recordAdd'
    ) {
        await next()
    } else {
        // 否则获取到token
        let token = ctx.request.headers["authorization"]

        if (token) {
            try {
                // 如果有token的话就开始解析
                const tokenItem = jwt.verify(token, 'itliuyang')
                // 将token的创建的时间和过期时间结构出来
                console.log('tokenItem=>',tokenItem);
                const { iat, exp } = tokenItem
                // 拿到当前的时间
                let data = new Date().getTime();
                // 判断一下如果当前时间减去token创建时间小于或者等于token过期时间，说明还没有过期，否则过期
                if (data / 1000 - iat <= exp) {
                    // token没有过期
                    await next()
                } else {
                    ctx.body = {
                        status: 405,
                        message: 'token 已过期，请重新登陆'
                    }
                }
            } catch (error) {
                if (error.name == 'JsonWebTokenError') {
                    ctx.body = {
                        status: 405,
                        message: "无效的token"
                    }
                } else if (error.name == 'TokenExpiredError') {
                    ctx.body = {
                        status: 405,
                        message: 'token 已过期，请重新登陆'
                    }
                } else {
                    ctx.body = {
                        status: 405,
                        message: error
                    }
                }
            }

        } else {
            ctx.body = {
                status: 405,
                message: '请先登陆'
            }
        }
    }
}
module.exports = check